<?php
class user
{
private $user_id;
private $group_id;
private $name;
private $lastname;
private $email;
private $password;
private $gender;
private $birthday;
private $address;
private $telephone;
private $mobile;
private $fax;
private $regisdate;
private $active;
private $deleted;
function setInsert($gi,$nm,$ln,$em,$pw,$gt,$bd,$add,$tlp,$mb,$fax,$atv)
{
$this->user_id=NULL;
$this->group_id=trim($gi);
$this->name=trim($nm);
$this->lastname=trim($ln);
$this->email=trim($em);
$this->password=sha1(trim($pw));
$this->gender=trim($gt);
$this->birthday=trim($bd);
$this->address=addslashes(strip_tags(trim($add)));
$this->telephone=trim($tlp);
$this->mobile=trim($mb);
$this->fax=trim($fax);
$this->regisdate=(date(Y)+543)."-".date("m-d");
$this->active=$atv;
$this->deleted="0";
}
function setupdate($nm,$ln,$em,$gt,$bd,$add,$tlp,$mb,$fax)
{			
$this->name=trim($nm);
$this->lastname=trim($ln);
$this->email=trim($em);
$this->gender=trim($gt);
$this->birthday=trim($bd);
$this->address=addslashes(strip_tags(trim($add)));
$this->telephone=trim($tlp);
$this->mobile=trim($mb);
$this->fax=trim($fax);
}
function checkupdate()
{		
if(!empty($this->name) && !empty($this->lastname) && !empty($this->email) && verify_email($this->email) && !empty($this->gender) &&!empty($this->birthday) && !empty($this->address) && !empty($this->mobile) )
return TRUE; 			
else return FALSE;
}
function checkinsert()
{
if(!empty($this->group_id) && !empty($this->password) && unique_email($this->email) && $this->checkupdate() )return TRUE;
else return FALSE;
}
function insert()
{
//if($this->checkinsert())
//{
        $str= base64_encode($this->email);
        $this->email=$str;
$sql="INSERT INTO sme_user
VALUES ( 
'$this->user_id',
'$this->group_id',
'$this->name',
'$this->lastname',
'$this->email',
'$this->password',
'$this->gender',
'$this->birthday',
'$this->address',
'$this->telephone',
'$this->mobile',
'$this->fax',
'$this->regisdate',
'$this->active',
'$this->deleted'
)";   
if(mysql_query( $sql ))return TRUE;
else return FALSE;
//}
//else return FALSE;
}
function update($up)
{
if($this->checkupdate() && !empty($up))
{
 $this->email=base64_encode($this->email);

$sql="UPDATE sme_user  SET 
name='$this->name',
lastname='$this->lastname',
email='$this->email',
gender='$this->gender',
birthday='$this->birthday',
address='$this->address',
mobile='$this->mobile'
WHERE user_id ='$up' ";
if(mysql_query( $sql ))return TRUE;
else return FALSE;
}
else return FALSE;
}
function delete($del)
{
if(!empty($del))
{
$sql="UPDATE sme_user SET deleted = '1' WHERE user_id =$del  ";
if(mysql_query( $sql ))return TRUE;
else return FALSE;
}
else return FALSE;
}
function changepassword($user,$pwd)
{
if(!empty($user))
{
$sql="UPDATE sme_user  SET password = '$pwd'  WHERE user_id ='$user' ";
return mysql_query( $sql );

}else return FALSE;
}

function uniqueEmailOnUpdate($email,$oldemail)
{
$str1= base64_encode($email);
$str2= $oldemail;
$sql="SELECT COUNT(email)AS count FROM sme_user WHERE email='$str1' AND email<>'$str2'";
$recSql = mysql_query( $sql);
$record=mysql_fetch_assoc($recSql);
if($record["count"]>0)  return FALSE;
else return TRUE;
}
function emailOnDelete($email)
{		
$str= base64_encode($email);
$sql="SELECT deleted FROM sme_user WHERE email='$str'";
$recSql = mysql_query( $sql);
$record=mysql_fetch_assoc($recSql);
if($record["deleted"]==1)  return TRUE;
else return FALSE;
}
}
function unique_email($email)
{
$str= base64_encode($email);
$sql="SELECT * FROM sme_user WHERE email='$str' ";
$num =mysql_num_rows(mysql_query( $sql));
if($num==0)  return TRUE;
else return FALSE;
}
function redir($str)
{
echo  "<meta http-equiv='refresh' content='0;URL=$str'>";exit();
}
?>